<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<script>

    //复习：栈的封装
    function Stack(){

        this.items = [];

        //method
        Stack.prototype.enStack = function (element){
            return this.items.push(element);
        };

        Stack.prototype.deStack = function(){
            return this.items.pop();

        };

        Stack.prototype.front = function(){
            return this.items[this.items.length - 1];
        };

        Stack.prototype.isEmpty = function(){
            return this.items.length === 0;
        };
        Stack.prototype.size = function (){
            return this.items.length;
        };

        Stack.prototype.toString = function(){
            var resultString = "";
            for(var i=0;i<this.items.length;i++){
                resultString += this.items[i] +" ";

            }
            return resultString;
        };
    }

    //复习：队列的封装
    function Queue(){

        //property
        this.items = [];

        //method
        Queue.prototype.enQueue = function (element){
            return this.items.push(element);
        };

        Queue.prototype.deQueue = function (){
            return this.items.shift();
        };
        Queue.prototype.front = function(){
            return this.items[0];
        };
        Queue.prototype.size = function (){
            return this.items.length;

        };
        Queue.prototype.isEmpty = function(){
            return this.items.length === 0;
        };
        Queue.prototype.toString = function(){
            var resultString = "";
            for(var i = 0;i<this.items.length;++i){
                resultString += this.items[i]+ " ";

            }
            return resultString;
        };


    }

    //复习：优先级队列的封装
    function priorityQueue (){
        //类中类
        function Data(data,priority){
            this.data = data;
            this.priority = null;
        }
        //属性
        this.items = [];

        //method
        priorityQueue.prototype.enQueue  = function(data,priority){
            var queue = new Data(data,priority);

            var added = false;
            if(this.items.length === 0){
                this.items.unshift(queue);
            }else{
                for(var i=0;i<this.items.length;i++){
                    if(queue.priority < this.items[i].priority){
                        this.items.splice(i,0,queue);
                        added = true;
                        break;
                    }
                }
                if(!added){
                    //通过added属性判断数组中是否存在优先级大于将插入元素的，如果没有，added属性为false
                    this.items.push(queue);
                }
            }
        };

        priorityQueue.prototype.deQueue = function (){
            return this.items.shift();
        };
        priorityQueue.prototype.front = function(){
            return this.items[0].data;
        };
        priorityQueue.prototype.size = function (){
            return this.items.length;

        };
        priorityQueue.prototype.isEmpty = function(){
            return this.items.length === 0;
        };
        priorityQueue.prototype.toString = function(){
            var resultString = "";
            for(var i = 0;i<this.items.length;++i){
                resultString += this.items[i].data+ " ";
            }
            return resultString;
        };
    }

    var queue = new priorityQueue();
    queue.enQueue(123,22);
    console.log(queue.toString());


</script>
</body>
</html>